//
//  WebBrowserViewController.m
//  Project
//
//  Created by Suny on 14-11-22.
//  Copyright (c) 2014年 Suny. All rights reserved.
//

#import "WebBrowserViewController.h"

@interface WebBrowserViewController ()
@property(nonatomic, strong) UIWebView *webView;

@end

@implementation WebBrowserViewController
- (instancetype)initWithUrlString:(NSString *)urlString
{
    self = [super init];
    if (self)
    {
        self.urlString = urlString;
		self.hasNavigationBar = YES;
		
    }

    return self;
}

+ (instancetype)controllerWithUrlString:(NSString *)urlString
{
    return [[self alloc] initWithUrlString:urlString];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.webView = [[UIWebView alloc] initWithFrame:self.viewContentFrame];
	self.webView.delegate = self;
	self.navigationItem.leftBarButtonItem = self.defaultBackButtonItem;
//	self.navigationItem.rightBarButtonItem = self.defaultRefreshButtonItem;

    [self.view addSubview:self.webView];
	
	[self refreshData];
}

- (void)requestDataFromNet
{
    if (CHECK_VALID_STRING(self.urlString))
    {
        NSURL *url = [NSURL URLWithString:self.urlString];
		NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:request];

    }
}

 - (void)refreshData
{
	[super refreshData];
	[self requestDataFromNet];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
	[self.coverView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	[self.coverView stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
	if ([error code] == -1004)
    {
        [self showErrorMessage:@""];
    }
	[self.coverView stopAnimationWithRetryAction:@selector(requestDataFromNet) withActObject:self];

}
@end
